package com.celesea.auth.grade.v1.controller;

import com.celesea.auth.grade.entity.Grade;
import com.celesea.auth.grade.service.GradeService;
import com.celesea.auth.grade.v1.controller.request.PostRequest;
import com.celesea.auth.grade.v1.controller.request.PutRequest;
import com.celesea.auth.grade.v1.controller.resource.GradeResource;
import com.celesea.auth.role.entity.RoleCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JackYuan
 * @create 2017-10-26 23:30
 */
@Api(tags = "职级接口")
@RestController
@RequestMapping("v1/grades")
@PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @GetMapping
    @ApiOperation(value = "职级列表")
    public List<GradeResource> page() {
        List<Grade> lists = gradeService.selectGrades();
        return lists.stream().map(item->new GradeResource(item))
                .collect(Collectors.toList());
    }

    @PostMapping
    @ApiOperation(value = "保存职级")
    public GradeResource post(@RequestBody @Valid PostRequest request) {
        Grade grade = gradeService.insert(new Grade(request));
        return new GradeResource(grade);
    }

    @PutMapping
    @ApiOperation(value = "更新职级")
    public GradeResource put(@RequestBody @Valid PutRequest request) {
        Grade grade = gradeService.update(new Grade(request));
        return new GradeResource(grade);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询职级")
    public GradeResource get(@PathVariable String id) {
        Grade grade = gradeService.selectById(id);
        return new GradeResource(grade);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据id删除职级")
    public void delete(@PathVariable String id) {
        gradeService.doDelete(id);
    }

}
